package it.zqdszb.library.adapter.pagerAdapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by GH on 2018-05-13.
 */

public class MyViewPagerAdapter extends FragmentPagerAdapter {
	private final List<Fragment> mFragments = new ArrayList<>();//添加的Fragment的集合
	private final List<String> mFragmentsTitles = new ArrayList<>();//每个Fragment对应的title的集合
	public MyViewPagerAdapter(FragmentManager fm) {
		super(fm);
	}
	/**
	 * @param fragment      添加Fragment
	 * @param fragmentTitle Fragment的标题，即TabLayout中对应Tab的标题
	 */
	public void addFragment(Fragment fragment, String fragmentTitle) {
		mFragments.add(fragment);
		mFragmentsTitles.add(fragmentTitle);
	}

	@Override
	public Fragment getItem(int position) {
		//得到对应position的Fragment
		return mFragments.get(position);
	}

	@Override
	public int getCount() {
		//返回Fragment的数量
		return mFragments.size();
	}

	@Override
	public CharSequence getPageTitle(int position) {
		//得到对应position的Fragment的title
		return mFragmentsTitles.get(position);
	}
}
